package jnpf.fico.controller;

import cn.hutool.core.util.ObjectUtil;
import jnpf.base.ActionResult;
import jnpf.base.UserInfo;
import jnpf.base.service.IBaseSerialNumberService;
import jnpf.engine.util.ServiceAllUtil;
import jnpf.exception.DataException;
import jnpf.exception.WorkFlowException;
import jnpf.fico.entity.ZcBillOperateItemEntity;
import jnpf.fico.entity.ZcglCwBillOperateEntity;
import jnpf.fico.model.operate.ZcBillOperateItemVO;
import jnpf.fico.model.operate.ZcglCwBillOperateForm;
import jnpf.fico.service.ZcBillOperateItemService;
import jnpf.fico.service.ZcglCwBillOperateService;
import jnpf.util.JsonUtil;
import jnpf.util.RandomUtil;
import jnpf.util.StringUtil;
import jnpf.util.UserProvider;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @description:
 * @author: zhengxiong.jiang
 * @createDate: 2024-10-30
 * @version: 1.0
 */
@RestController
@RequestMapping("/api/workflow/form/ZcglCwBillOperate")
public class ZcglCwBillOperateController {

    @Autowired
    private ZcglCwBillOperateService zcglCwBillOperateService;

    @Autowired
    private ZcBillOperateItemService zcBillOperateItemService;

    @Autowired
    private IBaseSerialNumberService iBaseSerialNumberService;



    @Autowired
    private UserProvider userProvider;
    @Autowired
    private ServiceAllUtil serviceUtil;
    private boolean primaryKeyPolicy = true;

    @PostMapping
    public ActionResult create(@RequestBody @Valid Map<String,Object> data) throws DataException, WorkFlowException {
        zcglCwBillOperateService.uniqueAll(data,0);
        data.put("flowtaskid", RandomUtil.uuId());
        ZcglCwBillOperateForm zcglCwBillOperateForm = JsonUtil.getJsonToBean(data, ZcglCwBillOperateForm.class);
        UserInfo userInfo = userProvider.get();
        ZcglCwBillOperateEntity entity = JsonUtil.getJsonToBean(zcglCwBillOperateForm, ZcglCwBillOperateEntity.class);
        String flowId = zcglCwBillOperateForm.getFlowId();
        String status = zcglCwBillOperateForm.getStatus();
        Map<String, List<String>> candidateList = zcglCwBillOperateForm.getCandidateList();

        String mainId = RandomUtil.uuId();
        entity.setId(mainId);

        if(entity.getType().equals(ZcglCwBillOperateEntity.TYPE_1)){
            entity.setSerialNo(iBaseSerialNumberService.getSerialNumber("J"));
        }else if(entity.getType().equals(ZcglCwBillOperateEntity.TYPE_2)){
            entity.setSerialNo(iBaseSerialNumberService.getSerialNumber("T"));
        }
        entity.setBillId(entity.getBillId());
//        entity.setParentid(null);
        //发起状态为1
        entity.setFlowState("1");
        zcglCwBillOperateService.create(entity,status,flowId,candidateList,data);


        //子表数据
        if (ObjectUtil.isNotEmpty(zcglCwBillOperateForm.getZcbillitemList())) {
            List<ZcBillOperateItemVO> zcBillOperateItemList = zcglCwBillOperateForm.getZcbillitemList();
            String parentId = entity.getId();
            List<ZcBillOperateItemEntity> zcBillOperateItemEntityList = new ArrayList<>();
            for (ZcBillOperateItemVO item : zcBillOperateItemList) {

                 mainId = RandomUtil.uuId();
                item.setParentId(parentId);
                item.setId(mainId);
                ZcBillOperateItemEntity zcBillOperateItemEntity = JsonUtil.getJsonToBean(item, ZcBillOperateItemEntity.class);
                zcBillOperateItemEntityList.add(zcBillOperateItemEntity);
            }
            zcBillOperateItemService.saveBatch(zcBillOperateItemEntityList);
        }


        return ActionResult.success("0".equals(zcglCwBillOperateForm.getStatus())?"提交成功":"保存成功");
    }

    @PutMapping("/{id}")
    public ActionResult update(@PathVariable("id") String id,String flowtaskid,
                               @RequestBody @Valid Map<String,Object> data) throws DataException,WorkFlowException{
        String taskId = StringUtil.isNotEmpty(flowtaskid)?flowtaskid:id;
        data.put("flowtaskid",taskId);
        zcglCwBillOperateService.uniqueAll(data,1);
        ZcglCwBillOperateForm zcgl_cw_bill_operateForm = JsonUtil.getJsonToBean(data,ZcglCwBillOperateForm.class);
        ZcglCwBillOperateEntity info = zcglCwBillOperateService.getInfo(taskId);
        if(info!=null){
            UserInfo userInfo = userProvider.get();
            ZcglCwBillOperateEntity entity = JsonUtil.getJsonToBean(zcgl_cw_bill_operateForm, ZcglCwBillOperateEntity.class);
            String flowId = zcgl_cw_bill_operateForm.getFlowId();
            String status = zcgl_cw_bill_operateForm.getStatus();
            Map<String, List<String>> candidateList = zcgl_cw_bill_operateForm.getCandidateList();
            zcglCwBillOperateService.update(info.getId(),entity,status,flowId,candidateList,data);
            return ActionResult.success("0".equals(zcgl_cw_bill_operateForm.getStatus())?"提交成功":"保存成功");
        }
        return ActionResult.fail("更新失败，数据不存在");
    }

    /**
     * 获取减免详情接口
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public ActionResult getOperateInfo(@PathVariable("id") String id){
        ActionResult actionResult = zcglCwBillOperateService.getOperateInfo(id);
        return actionResult;
    }

}
